package net.khoroshev.wschat.web;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import net.khoroshev.wschat.model.SessionWrapper;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/sessionInfo")
public class SessionInfoController {
    private static Log log = LogFactory.getLog(SessionInfoController.class);
    @Resource(name="sessionsMap")
    Map<String, HttpSession> sessions;
    @RequestMapping(value="/list")
    public @ResponseBody Set<SessionWrapper> getSessions() {
        final HashSet<SessionWrapper> set = new HashSet<>();
        if (sessions != null)  {
            for (HttpSession s : sessions.values()) {
                set.add(new SessionWrapper(s));
            }
        }
        return set;
    }
    @RequestMapping(value="/usersOnline")
    public @ResponseBody Set<String> getUsers() {
        final HashSet<String> set = new HashSet<>();
        if (sessions != null)  {
            for (HttpSession s : sessions.values()) {
                set.add(new SessionWrapper(s).getUserName());
            }
        }
        return set;
    }
    
}
